home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 51 / Amiga Format CD51 (2000-03-10)(Future Publishing)(GB)[!][issue 2000-04].iso / -in_the_mag- / workbench / term_4.8 / extras / source / gtlayout-source.lha / LTP_ConvertNum.c < prev    next >
C/C++ Source or Header  |  1997-05-08  |  1KB  |  104 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1997 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. #include "Assert.h"
  15.  
  16. #ifdef DO_HEXHOOK
  17.  
  18.     // Check whether the buffer only contains a valid hex/binary/octal number...
  19.  
  20. BOOL
  21. LTP_ConvertNum(BOOL negAllowed,STRPTR buffer,LONG *value)
  22. {
  23.     ULONG    num;
  24.     LONG    neg;
  25.     LONG    ch;
  26.  
  27.     num = 0;
  28.  
  29.     if((buffer[0] == '-') && negAllowed)
  30.     {
  31.         neg = -1;
  32.  
  33.         buffer++;
  34.     }
  35.     else
  36.         neg = 1;
  37.  
  38.     if(((buffer[0] == '0') && ToUpper(buffer[1] == 'X')) || (buffer[0] == '$'))
  39.     {
  40.         if(*buffer++ != '$')
  41.             buffer++;
  42.  
  43.         while(ch = ToUpper(*buffer++))
  44.         {
  45.             num = num * 16;
  46.  
  47.             if((ch >= 'A') && (ch <= 'F'))
  48.                 num += (ch - 'A') + 10;
  49.             else
  50.             {
  51.                 if ((ch >= '0') && (ch <= '9'))
  52.                     num += ch - '0';
  53.                 else
  54.                     return(FALSE);
  55.             }
  56.         }
  57.     }
  58.     else
  59.     {
  60.         if(*buffer == '%')
  61.         {
  62.             buffer++;
  63.  
  64.             while(ch = *buffer++)
  65.             {
  66.                 if((ch < '0') || (ch > '1'))
  67.                     return(FALSE);
  68.                 else
  69.                     num = (num * 2) + (ch - '0');
  70.             }
  71.         }
  72.         else
  73.         {
  74.             if(*buffer == '&')
  75.             {
  76.                 buffer++;
  77.  
  78.                 while(ch = *buffer++)
  79.                 {
  80.                     if((ch < '0') || (ch > '7'))
  81.                         return(FALSE);
  82.                     else
  83.                         num = (num * 8) + (ch - '0');
  84.                 }
  85.             }
  86.             else
  87.             {
  88.                 while(ch = *buffer++)
  89.                 {
  90.                     if((ch < '0') || (ch > '9'))
  91.                         return(FALSE);
  92.                     else
  93.                         num = (num * 10) + (ch - '0');
  94.                 }
  95.             }
  96.         }
  97.     }
  98.  
  99.     *value = num * neg;
  100.  
  101.     return(TRUE);
  102. }
  103. #endif
  104.